<?php

namespace BXTR\AdminBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
 * This is the class that validates and merges configuration from your app/config files
 *
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
 */
class Configuration implements ConfigurationInterface
{
    /**
     * {@inheritDoc}
     */
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('bxtr_admin');

        $rootNode
            ->children()
                ->arrayNode('entities')
                    ->useAttributeAsKey('name')
                    ->prototype('array')
                        ->children()
                            ->enumNode('guess_fields')
                                ->defaultValue('both')
                                ->values(array('none', 'list','edit','both'))
                            ->end()
                            ->arrayNode('fields')
                                ->useAttributeAsKey('name')
                                ->prototype('array')
                                    ->children()
                                        ->enumNode('visibility')
                                            ->values(array('none', 'list','edit','both'))
                                            ->defaultValue('both')
                                        ->end()
                                        ->scalarNode('label')
                                            ->defaultValue(null)
                                        ->end()
                                        ->scalarNode('type')
                                            ->defaultValue(null)
                                        ->end()
                                        ->arrayNode('options')
                                            ->prototype('variable')
                                            ->end()
                                        ->end()
                                        ->scalarNode('transformer')
                                            ->defaultValue(null)
                                        ->end()
                                    ->end()
                                ->end()
                            ->end()
                            ->arrayNode('on_remove')
                                ->addDefaultsIfNotSet()
                                ->children()
                                    ->arrayNode('cascade')
                                        ->prototype('scalar')->end()
                                    ->end()
                                    ->arrayNode('setnull')
                                        ->prototype('scalar')->end()
                                    ->end()

                                    ->arrayNode('shift')
                                        ->prototype('scalar')->end()
                                    ->end()

                                ->end()
                            ->end()
                            ->scalarNode('class')->defaultValue(null)->end()
                            ->scalarNode('data_class')->isRequired()->end()
                            ->arrayNode('list_fields')
                                ->prototype('scalar')->end()
                            ->end()
                            ->arrayNode('list_labels')
                                //#->useAttributeAsKey('name')
                                //#->isRequired()
                                ->prototype('scalar')->end()
                            ->end()
                            ->scalarNode('controller')
                                ->defaultValue('Entity')
                            ->end()
                            ->arrayNode('routes')
                                ->addDefaultsIfNotSet()
                                ->children()
                                    ->scalarNode('base')
                                        ->defaultValue('bxtr_admin_index')
                                    ->end()
                                    // thoses are filled in the Extension.
                                    ->scalarNode('remove_popup')
                                        ->defaultValue(null)
                                    ->end()
                                    ->scalarNode('add')
                                        ->defaultValue(null)
                                    ->end()
                                    ->scalarNode('edit')
                                        ->defaultValue(null)
                                    ->end()
                                    ->scalarNode('list')
                                        ->defaultValue(null)
                                    ->end()
                                ->end()
                            ->end()
                            ->arrayNode('views')
                                ->addDefaultsIfNotSet()
                                ->children()
                                    ->scalarNode('list')
                                        ->defaultValue('BXTRAdminBundle:Layout:List.html.twig')
                                    ->end()
                                    ->scalarNode('edit')
                                        ->defaultValue('BXTRAdminBundle:Layout:Edit.html.twig')
                                    ->end()
                                ->end()
                            ->end()
                            ->arrayNode('l18n')
                                ->addDefaultsIfNotSet()
                                ->children()
                                    ->scalarNode('list_title')->defaultValue(null)->end()
                                    ->scalarNode('confirm_delete')->defaultValue(null)->end()
                                    ->scalarNode('add_title')->defaultValue(null)->end()
                                    ->scalarNode('edit_title')->defaultValue(null)->end()
                                    ->scalarNode('remove_title')->defaultValue(null)->end()
                                ->end()
                            ->end()
                        ->end()
                    ->end()
                ->end()
                ->arrayNode('linklist')
                    ->children()
                        ->arrayNode('categories')
                            ->prototype('array')
                                ->children()
                                    ->scalarNode('title')->end()
                                    ->arrayNode('entities')
                                        ->prototype('scalar')->end()
                                    ->end()
                                ->end()
                            ->end()
                        ->end()
                    ->end()
                ->end()
            ->end()
        ;

        return $treeBuilder;
    }
}
